﻿Shader "Custom/OilPaint"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }

    SubShader
    {
        CGINCLUDE

        #include "UnityCG.cginc"

        sampler2D _MainTex, _NoiseTex;
		float2 _MainTex_TexelSize;

		float2 _ScreenResolution;
		float _ResolutionValue;
		float _Radius;

        struct a2v
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };
        
        struct v2f 
        {
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
        };

        v2f vert(a2v v)
        {
            v2f i;
            i.pos = UnityObjectToClipPos(v.vertex);
            i.uv = v.uv;
            return i;
        }

        ENDCG

        pass
        {
            CGPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag

			float4 frag(v2f Input) : COLOR
			{
				//【1】根据设置的分辨率比值，计算图像尺寸
				float2 src_size = float2(_ResolutionValue / _ScreenResolution.x, _ResolutionValue / _ScreenResolution.y);
				
				//【2】获取坐标值
				float2 uv = Input.uv.xy;
 
				//【3】根据半径，计算出n的值
				float n = float((_Radius + 1) * (_Radius + 1));;
 
				//【4】定义一些参数
				float3 m0 = 0.0;  float3 m1 = 0.0;
				float3 s0 = 0.0;  float3 s1 = 0.0;
				float3 c;
 
				//【5】按半径Radius的值，迭代计算m0和s0的值
				for (int j = -_Radius; j <= 0; ++j)
				{
					for (int i = -_Radius; i <= 0; ++i)
					{
						c = tex2D(_MainTex, uv + float2(i, j) * src_size).rgb; 
						m0 += c; 
						s0 += c * c;
					}
				}
 
				//【6】按半径Radius的值，迭代计算m1和s1的值
				for (int j = 0; j <= _Radius; ++j)
				{
					for (int i = 0; i <= _Radius; ++i)
					{
						c = tex2D(_MainTex, uv + float2(i, j) * src_size).rgb; 
						m1 += c;
						s1 += c * c;
					}
				}
 
				//【7】定义参数，准备计算最终的颜色值
				float4 finalFragColor = 0.;
				float min_sigma2 = 100;
 
				//【8】根据m0和s0，第一次计算finalFragColor的值
				m0 /= n;
				s0 = abs(s0 / n - m0 * m0);
 
				float sigma2 = s0.r + s0.g + s0.b;
				if (sigma2 < min_sigma2) 
				{
					min_sigma2 = sigma2;
					finalFragColor = float4(m0, 1.0);
				}
 
				//【9】根据m1和s1，第二次计算finalFragColor的值
				m1 /= n;
				s1 = abs(s1 / n - m1 * m1);
 
				sigma2 = s1.r + s1.g + s1.b;
				if (sigma2 < min_sigma2) 
				{
					min_sigma2 = sigma2;
					finalFragColor = float4(m1, 1.0);
				}
 
				//【10】返回最终的颜色值
				return finalFragColor;
			}

            ENDCG
        }
        
    }
    FallBack "Diffuse"
}
